package org.yzh.scheduler.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.quartz.SchedulerException;
import org.sample.model.APIResult;
import org.sample.model.PageInfo;
import org.sample.model.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.yzh.scheduler.model.entity.ScheduleJobDO;
import org.yzh.scheduler.model.vo.Option;
import org.yzh.scheduler.service.ScheduleJobService;

import java.util.List;

@ApiResponses(value = {
        @ApiResponse(code = 400, message = "请求参数有误", response = APIResult.class),
        @ApiResponse(code = 500, message = "服务器异常", response = APIResult.class)}
)
@Api(description = "任务调度API")
@RequestMapping("schedule-job")
@RestController
public class ScheduleJobController {

    @Autowired
    private ScheduleJobService scheduleJobService;

    @ApiOperation(value = "分组列表")
    @GetMapping("group")
    public List<Option> group() {
        List<Option> options = scheduleJobService.groupNames();
        return options;
    }

    @ApiOperation(value = "列表")
    @GetMapping
    public Pagination<ScheduleJobDO> list(@RequestParam(required = false) String name,
                                          @RequestParam(required = false) String group,
                                          @RequestParam(required = false) Boolean disable,
                                          PageInfo pageInfo) throws SchedulerException {
        List<ScheduleJobDO> jobs = scheduleJobService.find(name, group, disable);
        return new Pagination(pageInfo, jobs);
    }

    @ApiOperation(value = "新增")
    @PostMapping("{group}/{name:.+}")
    public APIResult add(@PathVariable String name, @PathVariable String group, @Validated @RequestBody ScheduleJobDO job) throws SchedulerException {
        job.setJobName(name);
        job.setJobGroup(group);
        scheduleJobService.add(job);
        return new APIResult();
    }

    @ApiOperation(value = "保存")
    @PutMapping("{group}/{name:.+}")
    public APIResult update(@PathVariable String name, @PathVariable String group, @Validated @RequestBody ScheduleJobDO job) throws SchedulerException {
        job.setJobName(name);
        job.setJobGroup(group);
        scheduleJobService.update(job);
        return new APIResult();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("{group}/{name:.+}")
    public APIResult<Boolean> delete(@PathVariable String name, @PathVariable String group) {
        scheduleJobService.delete(name, group);
        return new APIResult();
    }

    @ApiOperation(value = "运行一次")
    @PostMapping("{group}/{name:.+}/run-once")
    public APIResult<Boolean> runOnce(@PathVariable String name, @PathVariable String group) {
        boolean result = scheduleJobService.runOnce(name, group);
        return new APIResult(result);
    }

    @ApiOperation(value = "暂停")
    @PostMapping("{group}/{name:.+}/pause")
    public APIResult<Boolean> pause(@PathVariable String name, @PathVariable String group) {
        boolean result = scheduleJobService.pause(name, group);
        return new APIResult(result);
    }

    @ApiOperation(value = "恢复")
    @PostMapping("{group}/{name:.+}/resume")
    public APIResult<Boolean> resume(@PathVariable String name, @PathVariable String group) {
        boolean result = scheduleJobService.resume(name, group);
        return new APIResult(result);
    }
}